from scipy.linalg import svd
import numpy as np

# latent semantic indexing
def LSI(dtm, k):
    '''dtm: document-term matrix
       k:   number of dimensions'''

    u, sigma, vt = svd(dtm)

    u2 = u[:, 0:k]
    sigma2 = np.diag(sigma[0:k])

    return np.dot(u2, sigma2)


# plot two first components
import matplotlib
import pylab
def scatter_plot(m):
    matplotlib.pyplot.scatter(m[:, 0], m[:, 1])
    matplotlib.pyplot.show()

# html
header = """
<html>
  <head>
    <meta charset="utf-8">
    <title>Ilus pealkiri
    </title>
    <link rel="stylesheet" type="text/css" href="mystyle.css" />
  </head>

  <body>
    <h1>Pealkiri
    </h1>"""

footer = """
  </body>
</html>
"""

def html(z, clusters, max_num=50):
    unique_cluster_names = set(clusters)
    string = ""
    for c in unique_cluster_names:
        string += "<div class=\"cl_" + str(c) + "\">" + "\n" + "<h2> Klaster: " + str(c) + "</h2>" + "\n"
        count = 1
        for i in range(0, len(z)):
            if clusters[i] == c:
                string += "<p>" + "[" + str(count) + "] " + z[i] + "</p>" + "\n"
                count += 1
            if count > max_num:
                break
        string += "</div>" + "\n"

    return header + "\n" + string + "\n" + footer

## minimal example
#dtm = np.random.random_integers(0,10,size=(40,3))
#result = LSI(dtm, 2)
#
#scatter_plot(result)
#
#z = ["tere, mina olen siin",
#     "tere, sina oled siin",
#     "wikipedia korpus"]
#clusters = [1, 1, 2]
#tmp = html(z, clusters)
#output = open("html_example.html", "w")
#output.write(tmp)
#output.close()

# example on real data
from pypatnlp import *
X = load_obj('tasks/tanel02_binary_pca/small.X')
X = X[0:1000]

result = LSI(X, 2)
scatter_plot(result)

z = load_obj('tasks/tanel02_binary_pca/small.z')
z = z[0:1000]
clusters = np.random.random_integers(0,4,size=1000)
tmp = html(z, clusters)
output = open("html_example.html", "w")
output.write(tmp.encode("utf-8"))
output.close()

